package com.axx.android.profile.like;

import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.ImageView;

import com.axx.android.R;
import com.axx.android.utils.ImageLoader;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sendtion.xrichtext.StringUtils;

import java.util.List;

/**
 * Created by Administrator on 2017/8/1 0001.
 */

public class LikeLetterAdapter extends BaseQuickAdapter<LikeLetter.CollectionListBean, BaseViewHolder> {
    public LikeLetterAdapter(@LayoutRes int layoutResId, @Nullable List<LikeLetter.CollectionListBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, LikeLetter.CollectionListBean item) {
        helper.setText(R.id.tv_title, item.getTitle());
        helper.setText(R.id.tv_up_cnt, item.getGood() + "");

        String image = hasImage(item.getText());
        if (!TextUtils.isEmpty(image)) {
            ImageLoader.loadImage(image,(ImageView)helper.getView(R.id.iv_image), R.drawable.ic_placeholder, mContext);

        } else {
            ImageLoader.loadImage(item.getPic(), (ImageView) helper.getView(R.id.iv_image), R.drawable.ic_placeholder, mContext);
        }

        ImageView mIvUp = helper.getView(R.id.iv_up);
        ImageView mIvLike = helper.getView(R.id.iv_like);

        helper.addOnClickListener(R.id.iv_like);
        helper.addOnClickListener(R.id.iv_up);

        if (item.getGoodStatus() == 1) {
            mIvUp.setSelected(true);
        } else {
            mIvUp.setSelected(false);
        }
        if (item.getCollectionStatus() == 1) {
            mIvLike.setSelected(true);
        } else {
            mIvLike.setSelected(false);
        }
    }

    private String hasImage(String html) {
        List<String> textList = StringUtils.cutStringByImgTag(html);
        String image = "";
        for (String text : textList) {
            if (text.contains("<img") && text.contains("src=")) {
                String imagePath = StringUtils.getImgSrc(text);
                if (imagePath.contains("http://axx.oss-cn-shenzhen.aliyuncs.com")) {
                    image = imagePath;
                    return image;
                }
            }
        }
        return image;
    }
}

